package cn.qiao.zk.product.entity;

import lombok.Data;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 * desc:
 * 这是一个实现服务的注册与发现的类
 * @author qiao
 * @date 2023年05月05日 21:09
 */
@Data
public class Register {

    private static final String SERVER_PATH = "/product";

    private static final String ZK_ADDRESS = "127.0.0.1:2181";

    private static final int ZK_TIMEOUT = 20000;


    public void register(String address){
        try{
            ZooKeeper zooKeeper = new ZooKeeper(ZK_ADDRESS,ZK_TIMEOUT, WatchEvent ->{

            });
            Stat stat = zooKeeper.exists(SERVER_PATH,false);
            if(stat==null){
                zooKeeper.create(SERVER_PATH,"".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            String path = address;
            //创建短暂的可排序的子节点
            zooKeeper.create(SERVER_PATH+"/instance",path.getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
